package com.sgb.kjwl.viewmodel.self.carMakers;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.CarInfo;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.self.BusinessSubscribeEntity;
import com.sgb.kjwl.model.entity.self.MyOfferEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.carMaster.BusinessOpportunitySubscriptionActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BusinessOpportunitySubscriptionModel extends AndroidViewModel {
    public SelfRepertory repertory;
    public MutableLiveData<List<CarInfo>> carsLiveDta = new MutableLiveData<>();
    public MutableLiveData<UserMsgEntity> userMsgLiveDta = new MutableLiveData<>();
    public MutableLiveData<List<BusinessSubscribeEntity>> refreshLiveDta = new MutableLiveData<>();
    public MutableLiveData<List<BusinessSubscribeEntity>> loadMoreLiveDta = new MutableLiveData<>();
    private int page = 1;
    private int limit = 15;//2.0.3改为15

    public BusinessOpportunitySubscriptionModel(@NonNull Application application) {
        super(application);
    }

    //货源列表
    public void myGoodsSource(BusinessOpportunitySubscriptionActivity activity, boolean lodMore) {
        page = lodMore ? page + 1 : 1;
        Map map = new HashMap();
        map.put("limit", limit);
        map.put("page", page);
        DialogHelper.showProgressDialog(activity, null, "加载中", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getLogisticsAPI().getBusinessSubscribeList(map).enqueue(new Callback<BaseEntity<ListResponse<List<BusinessSubscribeEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<BusinessSubscribeEntity>>>> call, Response<BaseEntity<ListResponse<List<BusinessSubscribeEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    if (!lodMore)
                        activity.setEmptyVisibility();//加载失败显示空界面
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (!lodMore) {
                        refreshLiveDta.postValue(response.body().getData().getList());
                    } else {
                        loadMoreLiveDta.postValue(response.body().getData().getList());
                    }

                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<BusinessSubscribeEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
/*        APIManager.getInstance().getSelfOwnerAPI().getSupplyGoods(map).enqueue(new Callback<BaseEntity<ListResponse<List<BusinessSubscribeEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<BusinessSubscribeEntity>>>> call, Response<BaseEntity<ListResponse<List<BusinessSubscribeEntity>>>> response) {

            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<BusinessSubscribeEntity>>>> call, Throwable t) {
            }
        });*/
    }

    //报价
    public void findOfferByGoodsSourceNo(BusinessOpportunitySubscriptionActivity activity, String no) {
        DialogHelper.showProgressDialog(activity, null, "加载中", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().findOfferByGoodsSourceNo(no).enqueue(new Callback<BaseEntity<MyOfferEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MyOfferEntity>> call, Response<BaseEntity<MyOfferEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showDialog(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MyOfferEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    public void getUserInfo(BusinessOpportunitySubscriptionActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getUserInfo().enqueue(new Callback<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserMsgEntity>> call, Response<BaseEntity<UserMsgEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {
                    userMsgLiveDta.postValue(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getCarInfo(BusinessOpportunitySubscriptionActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getCarInfo().enqueue(new Callback<BaseEntity<List<CarInfo>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarInfo>>> call, Response<BaseEntity<List<CarInfo>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    carsLiveDta.postValue(response.body().getData());
                } else {
                    MToast.showToast(activity.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarInfo>>> call, Throwable t) {

            }
        });
    }
}
